package com.wutianyi.task;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import static com.wutianyi.entity.EntityTypeEnum.*;

import org.apache.commons.io.FileUtils;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Transaction;
import com.wutianyi.utils.Utils;

public class ChinaGeoTask implements Tasks {

	public static void main(String[] args) throws IOException {

	}

	private static void nextLevelEntities(Map<String, String> values,
			Map<String, List<String>> children, List<String> childs,
			Entity parent, DatastoreService datastore, String type) {
		if (null == childs || 0 == childs.size()) {
			return;
		}

		for (String c : childs) {
			String v = Utils.decodeUnicode(values.get(c));
			Entity childEntity = new Entity(type, v, parent.getKey());
			childEntity.setProperty("name", v);
			datastore.put(childEntity);

			List<String> cs = children.get(c);
			if (null != cs && cs.size() > 0) {
				nextLevelEntities(values, children, cs, childEntity, datastore,
						Area.name());
			}
		}
	}

	@Override
	public void run() {
		String content = null;
		try {
			content = new String(
					FileUtils.readFileToByteArray(new File(ChinaGeoTask.class
							.getResource("/data/allgeos").getFile())), "utf-8");
			Map<String, String> values = new HashMap<String, String>();
			Map<String, List<String>> children = new HashMap<String, List<String>>();
			String[] cs = content.split(";");
			for (String c : cs) {
				String[] vs = c.split(":");
				String id = vs[0].substring(1, vs[0].length() - 1);
				String[] tvs = vs[1].split(",");
				String value = tvs[0].substring(1, tvs[0].length() - 1);
				String p = tvs[1].substring(1, tvs[1].length() - 1);

				values.put(id, value);
				List<String> childrs = children.get(p);
				if (null == childrs) {
					childrs = new ArrayList<String>();
					children.put(p, childrs);
				}
				childrs.add(id);
			}
			List<String> provinces = children.get("1");
			Entity country = new Entity(Country.name(), "中国");
			DatastoreServiceFactory.getDatastoreService().put(country);
			for (String p : provinces) {
				DatastoreService datastore = DatastoreServiceFactory
						.getDatastoreService();
				Transaction tx = datastore.beginTransaction();
				try {
					String v = Utils.decodeUnicode(values.get(p));
					Entity province = new Entity(Province.name(), v, country.getKey());
					province.setProperty("name", v);
					datastore.put(province);

					nextLevelEntities(values, children, children.get(p),
							province, datastore, City.name());

					tx.commit();
				} finally {
					if (tx.isActive()) {
						tx.rollback();
					}
				}

			}

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void run(HttpServletRequest request) {
		run();
	}

}
